<?php

namespace App\Model;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];


    public static function getInfo($uid){
        $userInfo = Session::get("userinfo");
        if(empty($userInfo)){
            $userInfo = DB::table("users")->where("uid",$uid)->first();
            Session::put("userinfo",json_encode($userInfo));
            return $userInfo;
        }
        return json_decode($userInfo,true);
    }

    public static function create($data){
        $avators = DB::select("select * from avators order by rand() limit 1");
        $insert['user_name'] =$data['user_name'];
        $insert['email'] =isset($data['email'])?$data['email']:"";
        $insert['mobile'] =isset($data['mobile'])?$data['mobile']:"";
        $insert['password'] = Hash::make($data['password']);
        $insert['avator'] =$avators[0]->avator;
        $insert['create_at'] =date("Y-m-d H:i:s",time());
        $insert['update_at'] =date("Y-m-d H:i:s",time());
        return DB::table("users")->insertGetId($insert);
    }


    public static function updatePassword($data,$password){
        $insert['password'] = $password;
        $insert['update_at'] =date("Y-m-d H:i:s",time());
        return DB::table("users")->where("email",$data['email'])->update($insert);
    }



}
